<template>
  <view class="content">
    <!-- <mescroll-uni
      ref="mescrollRef"
      @init="mescrollInit"
      @up="upCallback"
      :up="upOption"
      :down="{ use: false }"
    > -->
    <view class="section-0">
      <view class="section-body">
        <view class="list">
          <view class="wrapper">
            <view class="person"
              >招标
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.bidding"
                inputAlign="center"
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="form-item">
              <text class="label">计划上报时间</text>
              <view class="value">
                <view class="date">
                  <view class="input-body" @click="handleShowTime(0)">{{
                    time[0].value
                  }}</view>
                  <view class="iconfont icon-rili"></view>
                </view>
              </view>
            </view>
          </view>
          <view class="wrapper">
            <view class="form-item">
              <text class="label">合同签订时间</text>
              <view class="value">
                <view class="date">
                  <view class="input-body" @click="handleShowTime(1)">{{
                    time[1].value
                  }}</view>
                  <view class="iconfont icon-rili"></view>
                </view>
              </view>
            </view>
          </view>
          <view class="wrapper">
            <view class="person"
              >合同名称
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.contract_name"
                inputAlign="center"
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="person"
              >合同乙方
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.partner"
                inputAlign="center"
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="person"
              >合同金额
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.contract_amount"
                inputAlign="center"
                type="number"
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="person"
              >累计支出金额
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.grand_amount"
                inputAlign="center"
                disabled
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="person"
              >累计支付比例
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                v-model="form.pay_proportion"
                inputAlign="center"
                disabled
              ></u--input
            ></view>
          </view>
          <view class="wrapper">
            <view class="person"
              >本期支付金额
              <u--input
                placeholderClass="input-placeholder"
                class="input-body"
                placeholder="请输入本期支付金额"
                v-model="form.pay_amount"
                inputAlign="center"
                type="number"
              ></u--input
            ></view>
          </view>
        </view>
        <u-datetime-picker
          :show="showStart"
          v-model="startStamp"
          closeOnClickOverlay
          mode="date"
          @confirm="confirmStart"
          @cancel="showStart = false"
          @close="showStart = false"
        ></u-datetime-picker>
      </view>
    </view>
    <view class="preservation">
      <u-button
        class="preservabtn"
        type="primary"
        text="保存"
        @click="submit"
      ></u-button>
    </view>
    <!-- </mescroll-uni> -->
  </view>
</template>

<script>
import { modifyContractDetails, contractDetails } from "@/api/project.js";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      time: [{ value: "" }, { value: "" }],
      form: {
        bidding: "",
        contract_name: "",
        partner: "",
        contract_amount: "",
        grand_amount: "",
        pay_proportion: "",
        pay_amount: "",
      },
      curIdx: "",
      start: "",
      id: "",
      startStamp: Number(new Date()),
      showStart: false,
      list: [],
      upOption: {
        textNoMore: "-- 没有更多了 --",
      },
    };
  },

  computed: {
    ...mapGetters({
      curProject: "system/getCurProject",
    }),
  },
  created() {
    this.upCallback();
  },
  onLoad(data) {
    this.id = parseInt(data.params);
    console.log(typeof this.id);
  },
  methods: {
    async upCallback() {
      const data = await contractDetails({
        id: this.id,
      });
      console.log(data.data.data);
      this.time[0].value = data.data.data.report_time.substring(0, 10);
      this.time[1].value = data.data.data.sign_time.substring(0, 10);
      this.form.bidding = data.data.data.bidding;
      this.form.contract_name = data.data.data.contract_name;
      this.form.partner = data.data.data.partner;
      if (
        data.data.data.grand_amount == "" ||
        data.data.data.grand_amount == null ||
        data.data.data.grand_amount == undefined
      ) {
        data.data.data.grand_amount = 0;
      }
      this.form.contract_amount = data.data.data.contract_amount;
      this.form.grand_amount = data.data.data.grand_amount;
      this.form.pay_proportion =
        Math.round(
          (data.data.data.grand_amount / data.data.data.contract_amount) * 10000
        ) /
          100 +
        "%";
      this.form.pay_amount = data.data.data.pay_amount;
    },

    async submit() {
      if (
        parseFloat(this.form.pay_amount) + parseFloat(this.form.grand_amount) >
        parseFloat(this.form.contract_amount)
      ) {
        uni.showToast({
          title: "累计支出金额不能大于合同金额",
          duration: 1000,
          icon: "error",
        });
        return;
      }
      if (this.form.contract_name == "") {
        uni.showToast({
          title: "请填写合同名称",
          duration: 1000,
          icon: "error",
        });
        return;
      }
      if (this.form.partner == "") {
        uni.showToast({
          title: "请填写合同乙方",
          duration: 1000,
          icon: "error",
        });
        return;
      }
      if (this.form.contract_amount == "") {
        uni.showToast({
          title: "请填写合同金额",
          duration: 1000,
          icon: "error",
        });
        return;
      }
      const data = await modifyContractDetails({
        id: this.id,
        project_id: this.curProject.id,
        report_time: this.time[0].value,
        sign_time: this.time[1].value,
        bidding: this.form.bidding,
        contract_name: this.form.contract_name,
        partner: this.form.partner,
        contract_amount: this.form.contract_amount,
        grand_amount:
          parseFloat(this.form.pay_amount) + parseFloat(this.form.grand_amount),
      });
      console.log(data.data);
      if (data.data.code == 1) {
        uni.showToast({
          title: "保存成功",
          duration: 1000,
          icon: "success",
        });
        uni.hideLoading();
        this.Back();
      } else {
        uni.showToast({
          title: "保存失败，请重试",
          duration: 1000,
          icon: "error",
        });
        uni.hideLoading();
      }
    },

    Back() {
      uni.$emit("refreshData");
      uni.navigateBack({
        delta: 1,
      });
    },

    confirmStart(val) {
      const timeStamp = val.value;
      this.time[this.curIdx].value = this.formatDate(timeStamp);
      console.log(this.time[this.curIdx].value);
      this.showStart = false;
    },
    handleShowTime(idx) {
      this.curIdx = idx;
      console.log(this.curIdx);
      this.showStart = true;
    },
    formatDate(timeStamp) {
      const date = new Date(timeStamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      const Y = date.getFullYear() + "-";
      const M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      const D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      const h = date.getHours() + ":";
      const m = date.getMinutes() + ":";
      const s = date.getSeconds();
      return Y + M + D;
    },
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  width: 100%;
  border-radius: 35rpx;
  background-size: cover;
  background-repeat: no-repeat;
  overflow: hidden;
  box-sizing: border-box;
  box-shadow: 0 0 20rpx 10rpx rgba(#bcbcb5, 0.17);
  padding: 28rpx 40rpx;
  padding-top: 15rpx;
  margin-bottom: 40rpx;
  font-size: 27rpx;
  .person {
    font-weight: bolder;
    padding-top: 20rpx;
    .input-body {
      box-sizing: border-box;
      height: 55.6rpx;
      width: 408rpx !important;
      font-weight: normal;
      margin-top: -44rpx !important;
      border: 1px solid #1534ce !important;
      border-radius: 28.8rpx;
      padding-left: 30rpx !important;
      padding-right: 30rpx !important;
      margin-left: 197rpx;
      text-align: center !important;
    }
    .uni-input-input {
      text-align: center !important;
    }
  }
}

.section-0 {
  width: 100%;
  position: relative;
  top: -50rpx;
  z-index: 10;
  background-color: #2e5efe;
  margin-top: 17rpx;
  .section-body {
    width: 100%;
    height: 100%;
    margin-top: 50rpx;
    border-top-left-radius: 50rpx;
    border-top-right-radius: 50rpx;
    background-color: #fff;
    overflow: hidden;
    padding-top: 46rpx;
    .label {
      margin-left: -32rpx;
      margin-top: 21rpx;
      color: #000 !important;
      font-weight: bolder;
    }
    .input-body {
      height: 55.6rpx;
      width: 501rpx;
      border: 1px solid #1534ce;
      border-radius: 28.8rpx;
      margin-top: 13.5rpx;
      text-align: center !important;
    }
    .uni-input-input {
      text-align: center !important;
    }
    .list {
      margin-bottom: 150rpx;
      width: 670rpx;
      box-sizing: border-box;
      margin-left: 40rpx;
      .form-item {
        width: 662rpx;
        height: 48rpx;
        border-radius: 20rpx;
        margin: 0 auto;
        margin-bottom: 25rpx;

        @include clear();
        .label {
          float: left;
          width: 209rpx;
          height: 48rpx;
          text-align: center;
          line-height: 44rpx;
          font-size: 26rpx;
          font-stretch: normal;
          letter-spacing: 0rpx;
          margin-left: -27rpx;
          margin-top: 21rpx;
          color: #000 !important;
          font-weight: bolder;
        }
        .icon-rili {
          margin-left: 551rpx;
          margin-top: -44rpx;
          font-size: 33rpx;
          color: #1534ce;
        }
        .input-body {
          box-sizing: border-box;
          padding: 0 !important;
          padding-left: 30rpx !important;
          padding-right: 30rpx !important;
          font-size: 26rpx;
          font-weight: normal;
          font-stretch: normal;
          letter-spacing: 0rpx;
          text-align: center;
          font-size: 28rpx;
          color: #1534ce;
          line-height: 55.6rpx;
          height: 55.6rpx !important;
          width: 408rpx !important;
          border: 1px solid #1534ce !important;
          border-radius: 28.8rpx !important;
          margin-top: 13.5rpx !important;
          margin-left: 197rpx;
        }
        .input-placeholder {
          font-size: 26rpx;
          font-weight: normal;
          font-stretch: normal;
          letter-spacing: 0rpx;
          color: rgba(0, 0, 0, 0.5);
        }
      }

      .item__expand {
        .parent {
          .action {
            .iconfont {
              transform: rotate(-90deg);
            }
          }
        }

        .child {
          box-shadow: 0rpx 10rpx 10rpx 0rpx rgba(0, 0, 0, 0.03) inset;
        }
      }
    }
  }
}
.preservation {
  width: 100%;
  z-index: 9999;
  height: 115rpx;
  position: fixed;
  bottom: 0rpx;
  right: 0;
  padding-top: 21rpx;
  box-shadow: 0 0 10rpx 10rpx rgba(#bcbcb5, 0.17);
  .preservabtn {
    width: 75%;
    margin: auto;
  }
}
</style>
